
#include <jem/base/PrecheckException.h>

#include "utilities.h"


//-----------------------------------------------------------------------
//   constants
//-----------------------------------------------------------------------


const int STRAIN_COUNTS[4] = { 0, 1, 2, 3 };


//-----------------------------------------------------------------------
//   get1DShapeGrads
//-----------------------------------------------------------------------


void              get1DShapeGrads

  ( const Matrix&   b,
    const Matrix&   g )

{
  JEM_PRECHECK ( b.size(0) == 1 &&
                 g.size(0) == 1 &&
                 b.size(1) == g.size(1) );

  b = g;
}


//-----------------------------------------------------------------------
//   get2DShapeGrads
//   for PhaseField Problem
//  B =  | N,1 |
//       | N,2 |
//
//-----------------------------------------------------------------------


void              get2DShapeGrads

  ( const Matrix&   b,
    const Matrix&   g )

{
  JEM_PRECHECK ( b.size(0) == 2 &&
                 g.size(0) == 2 &&
                 b.size(1) == g.size(1) );

  b = g;

}


//-----------------------------------------------------------------------
//   get3DShapeGrads
//-----------------------------------------------------------------------


void              get3DShapeGrads

  ( const Matrix&   b,
    const Matrix&   g )

{
  JEM_PRECHECK ( b.size(0) == 6 &&
                 g.size(0) == 3 &&
                 b.size(1) == 3 * g.size(1) );

  const int  nodeCount = g.size (1);


  b = 0.0;

  for ( int inode = 0; inode < nodeCount; inode++ )
  {
    int  i = 3 * inode;

    b(0,i + 0) = g(0,inode);
    b(1,i + 1) = g(1,inode);
    b(2,i + 2) = g(2,inode);

    b(3,i + 0) = g(1,inode);
    b(3,i + 1) = g(0,inode);

    b(4,i + 1) = g(2,inode);
    b(4,i + 2) = g(1,inode);

    b(5,i + 2) = g(0,inode);
    b(5,i + 0) = g(2,inode);
  }
}


//-----------------------------------------------------------------------
//   getShapeGradsFunc
//-----------------------------------------------------------------------


ShapeGradsFunc getShapeGradsFunc ( int rank )
{
  JEM_PRECHECK ( rank >= 1 && rank <= 3 );


  if      ( rank == 1 )
  {
    return & get1DShapeGrads;
  }
  else if ( rank == 2 )
  {
    return & get2DShapeGrads;
  }
  else
  {
    return & get3DShapeGrads;
  }
}
